In [10]:
# Solution One: Triple Quotes!!
cool_story_bro = """"Ahhh!!!! spiders!", cried the monster."Don't worry" said our hero, "I have a sharp spoon"."""
print(cool_story_bro)
I’ll be honest, the above solution of using triple-quotes isn’t something I knew about before making the guide. As it turns out this is a really neat solution I think, its clean, readable, and above all simple.
In [11]:
# Solution Two: Escape characters...
cool_story_bro = '"Ahhh!!!! spiders!", cried the monster."Don\'t worry" said our hero, "I have a sharp spoon".'
print(cool_story_bro)
For this solution we use the single quote character to enclose the string and then we use escape on the ' character in the word “Don't”.
“...Don’t...” ---> “...Don\’t...”
Another clean and simple solution.
In [14]:
# Solution Three: Splitting up the string into seperate parts...
a = '"Ahhh!!!! spiders!", cried the monster."Don'
b = "'t "
c = "worry"
d = '" said our hero, "I have a sharp spoon".'
cool_story_bro = a + b + c + d
print(cool_story_bro)
This third solution of splitting the word up into parts is just plain terrible compared to the other two ways of doing it; its less readable, more complex and above all else utterly necessary.
As for the best method to use, I think its a close race between solution #1 and #2, but I think #1 wins it for me because this solution is a bit more scalable. I mean think about it, if the string we are handling is part of a book we have to process then its possible that there are going to be hundreds of ‘ in the document; David’s spoon, can’t, won’t, o’clock are just a tiny handful of possible phrases that could exist. Do we really want to go through a manuscript to find and then correct such instances? Triple quotes are simply more economical.